<?php
namespace app\admin\controller;

use \think\Request;
use think\facade\View;
use app\common\model\Goods;
use app\common\model\GoodsList;
use app\common\model\Shang;
use app\common\model\User;
use app\common\model\Climb as ClimbModel;

class Climb extends Base{

    /**
     * 攀登
     */
    public function index(Request $request){
        $keyword=trim(input('get.keyword'));
        $status=trim(input('get.status'));
        $whe=array();
        if(!empty($keyword)){
            $whe[]=['title','like','%'.$keyword.'%'];
        }
        if(!empty($status)){
            $whe[]=['status','=',$status];
        }else{
            $whe[]=['status','<','4'];
        }
        $whe[] = ['type','=','11'];
        $field = "*";
        $order = "sort asc,id desc";
        $data = Goods::getList($whe,$field,$order,$this->page);
        foreach ($data['list'] as $k => &$v) {
            if ($v['addtime'] > 0) {
                $v['addtime'] = date('Y-m-d H:i:s', $v['addtime']);
            } else {
                $v['addtime'] = '不限制';
            }
            if ($v['end_time'] > 0) {
                $v['end_time'] = date('Y-m-d H:i:s', $v['end_time']);
            }else {
                $v['end_time'] = '不限制';
            }
        }
        View::assign('list',$data['list']);
        View::assign('count',$data['count']);
        View::assign('page',$data['page']);
        return View::fetch("Climb/index");
    }

    /**
     * 创建攀登盒子
     */
    public function add(Request $request){
        if(!$request->isPost()){
            return View::fetch("Climb/add");
        }else{
            $data = input('post.');
            if(empty($data['title'])){
                return $this->renderError("请输入赛季名称");
            }
            if(empty($data['addtime'])){
                return $this->renderError("请选择赛季开始时间");
            }
            if(empty($data['end_time'])){
                return $this->renderError("请选择赛季结束时间");
            }  
            if(empty($data['floors_num'])){
                return $this->renderError("请输入爬塔层数");
            }
            if($data['floors_num'] < 10 || $data['floors_num'] > 40){
                return $this->renderError("爬塔层数范围10-40");
            }
            
            $data['addtime'] =  strtotime($data['addtime']);
            $data['end_time'] = strtotime($data['end_time']);
            $data['status'] = 0;
            $data['type'] = 11;
            Goods::startTrans();
            $dd = Goods::insertGetId($data);
            $arr = [];
            for($i=0; $i<$data['floors_num']; $i++){
                $m = 0 ;
                $arr[$i]['goods_id'] = $dd;
                $m = $i+1;
                $arr[$i]['num'] = $m;
                $arr[$i]['price'] = 0;
                $arr[$i]['title'] = "第".$m."层";
                $arr[$i]['update_time'] = time();
            }
            $ss = ClimbModel::insertAll($arr);
            if($dd && $ss){
                Goods::commit();
                return $this->renderSuccess("添加成功");
            }else{
                Goods::rollback();
                return $this->renderError("添加失败");
            }
        }
    }

    /**
     * 编辑
     */
    public function edit(Request $request){
        if(!$request->isPost()){
            $id = $request->param('goods_id/d');
            $w = [];
            $w[] = ['id','=',$id];
            $data = Goods::where(['id'=> $id])->find();
            View::assign("data",$data);
            return View::fetch("Climb/edit");
        }else{
            $data = input('post.');
            if(empty($data['title'])){
                return $this->renderError("请输入盒子名称");
            }
            if(empty($data['addtime'])){
                return $this->renderError("请选择赛季结束时间");
            }
            if(empty($data['end_time'])){
                $data['end_time'] = 0;
            }
            $climb = Goods::where(['id'=>$data['id']])->find();
            
            if ($data['floors_num'] < $climb['floors_num']) {
                $where = [];
                $where[] = ['goods_id','=', $climb['id']];
                $where[] = ['num', '>', $data['floors_num']];
                $ret = ClimbModel::where($where)->delete();
            }
            $data['addtime'] =  strtotime($data['addtime']);
            $data['end_time'] = strtotime($data['end_time']);
            $data['status'] = 0;
            $data['type'] = 11;
            $dd = Goods::where(['id'=>$data['id']])->update($data);
            if($dd){
                return $this->renderSuccess("编辑成功");
            }else{
                return $this->renderError("编辑失败");
            }
        }
    }

    /**
     * 操作
     */
    public function cao(Request $request){
        $id=$request->post('id/d');
        $status=$request->post('type/d');
        if(!in_array($status,array(2,3,4,1))){
            return $this->renderError("传参错误1");
        }
        $data = Goods::where(['id'=>$id])->field('id,title')->find();
        if(!$data){
            return $this->renderError("数据不存在");
        }
        // if($status==1){
        //     $w = [];
        //     $w[] = ['type','=','11'];
        //     $w[] = ['status','=','1'];  //上架中
        //     $w[] = ['id','<>',$id];
        //     $count = Goods::where($w)->count('id');
        //     if($count > 0){
        //         return $this->renderError("已有进行中的爬塔");
        //     }
        // }
        $result = Goods::where(['id'=>$id])->update(["status"=>$status]);
        if($result){
            return $this->renderSuccess("操作成功");
        }else{
            return $this->renderError("网络繁忙，请稍后");
        }
    }

    /**
     * 一键复制
     */
    public function copy(Request $request){
        $id = $request->param('id');
        $w = [];
        $w[] = ['id','=',$id];
        $goods = Goods::getInfo($w,'*');
        $goods = $goods->toArray();
        $ww = [];
        $ww[] = ['goods_id','=',$id];
        $ww[] = ['status','=','1'];
        $goodslist = GoodsList::getAllList($ww,'*');
        unset($goods['id']);
        $chazhi = $goods['end_time'] - $goods['addtime'];
        $goods['addtime'] = $goods['end_time'] + 1;
        $goods['end_time'] = $goods['addtime'] + $chazhi;
        $goods['status'] = 1;
        goodsList::startTrans();
        $dd = goods::insertGetID($goods);
        $goodslist = $goodslist->toArray();
        foreach($goodslist as $k=>&$v){
            unset($v['id']);
            $v['goods_id'] = $dd;
            $v['stock'] = $v['gailv'];
            $time = time()+$k;
            $w = [];
            $w[] = ['addtime','=',$time];
            $res = goodsList::getInfo($w,'id,title');
            if(!empty($res)){
                goodsList::rollback();
                return $this->renderError("复制过快，请稍后在复制");
            }
            $v['addtime'] = $time;
        }
        $ss = goodsList::insertAll($goodslist);
        $where=[];
        $where[] = ['goods_id', '=', $id];
        $climbs = ClimbModel::getAllList($where);
        $climbs = $climbs->toArray();
        foreach ($climbs as $k => &$v) {
            unset($v['id']);
            $v['goods_id'] = $dd;
            $v['update_time'] = time()+$k;
        }
        $hh = ClimbModel::insertAll($climbs);
        if($dd && $ss && $hh){
            goodsList::commit();
            return $this->renderSuccess("复制成功");
        }else{
            InfiniteList::rollback();
            return $this->renderError("复制失败");
        }
    }
    

    
}